﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;

namespace Drifting.Graphics.PhysicalObjects
{
    public class UnitBall : PhysicalObject
    {
        double radius;

        public UnitBall(Game1 game)
        {
            Body = game.Content.Load<Model>("models\\UnitBall");
            Body_AbsoluteMatrix = new Matrix[Body.Bones.Count];
            Body.CopyAbsoluteBoneTransformsTo(Body_AbsoluteMatrix);

            body_scale = 0.0255f;
        }

        public UnitBall(Game1 game, float radius)
        {
            Body = game.Content.Load<Model>("models\\UnitBall");
            Body_AbsoluteMatrix = new Matrix[Body.Bones.Count];
            Body.CopyAbsoluteBoneTransformsTo(Body_AbsoluteMatrix);

            this.radius = radius;
            body_scale = (float)(0.0255f * this.radius);
        }

        public double Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                body_scale = (float)(0.0255f * this.radius);
            }
        }

        public void Draw(Matrix camera, Matrix projection, float x, float y, float z)
        {

            Matrix config = Matrix.CreateTranslation(x, y, z);
            
            foreach (ModelMesh mesh in Body.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.View = camera;
                    effect.Projection = projection;
                    effect.World =
                        Body_AbsoluteMatrix[mesh.ParentBone.Index] *
                        Matrix.CreateScale(body_scale) *
                        config;
                }
                mesh.Draw();
            }
        }
    }
}
